#include "com_parse_task.h"
#include "app_service.h"
#include "server_service.h"
#include "com_service.h"

void com_parse_task::run( )
{
	bool b_flag = true ;
	Value root ;
	
	switch (mv_data[1])
	{
/*	
		case 0x00 :
		{
			break ;
		}
*/		
		case 0x01 :
		{
			switch(mv_data[2])
			{
				case 0x84:		//5.2.1
				{
					char s_speed_min[10] = { 0x00 } ;
					sprintf( s_speed_min, "%d", (unsigned int)mv_data[3] ) ;
					root["speed_min"] = s_speed_min;
					
					char s_speed_max[10] = { 0x00 } ;
					sprintf( s_speed_max, "%d", (unsigned int)mv_data[4] ) ;
					root["speed_max"] = s_speed_max;

					char s_grade_max[10] = { 0x00 } ;
					sprintf( s_grade_max, "%d", (unsigned int)mv_data[5] ) ;
					root["grade_max"] = s_grade_max;

					char s_vender_id[10] = { 0x00 };
					sprintf( s_vender_id, "%02x%02x", mv_data[6], mv_data[7]) ;
					root["vender_id"] = s_vender_id;
					(com_service::_instance()).m_task.ms_vender = s_vender_id ;
					
					char s_machine_id[10] = { 0x00 };
					sprintf( s_machine_id, "%02x%02x", mv_data[8], mv_data[9]) ;
					root["machine_id"] = s_machine_id;
										
					char s_ble_serial_no[20] = { 0x00 };
					sprintf( s_ble_serial_no, "%02x%02x%02x%02x%02x%02x", mv_data[10], mv_data[11], mv_data[12], mv_data[13], mv_data[14], mv_data[15]) ;
					root["ble_serial_no"] = s_ble_serial_no;
					if( mv_data[10] == 0x00
						&& mv_data[11] == 0x00
						&& mv_data[12] == 0x00
						&& mv_data[13] == 0x00
						&& mv_data[14] == 0x00
						&& mv_data[15] == 0x00 )
						(com_service::_instance()).m_task.ms_sn = "" ;
					else
						(com_service::_instance()).m_task.ms_sn =s_ble_serial_no ;

					char s_control_version[10] = { 0x00 };
					sprintf( s_control_version, "%02x%02x", mv_data[16], mv_data[17]) ;
					root["control_version"] = s_control_version ;
					break ;
				}
				case 0x8b:		//5.2.2
				{
					char s_arg_set_reply[10] = { 0x00 } ;
					sprintf( s_arg_set_reply, "%d", (unsigned int)mv_data[3] ) ;
					root["arg_set_reply"] = s_arg_set_reply;
					break ;
				}
				case 0x82:		//5.2.3
				{
					char s_person_info[10] = { 0x00 } ;
					sprintf( s_person_info, "%d", (unsigned int)mv_data[3] ) ;
					root["person_info"] = s_person_info ;
					break ;
				}
				case 0x87:		//5.2.4
				{		
					char s_set_speed[10] = { 0x00 } ;
					sprintf( s_set_speed, "%.2f", (float)(((float)mv_data[3])/10) ) ;
					root["set_speed"] = s_set_speed;

					char s_set_grade[10] = { 0x00 } ;
					sprintf( s_set_grade, "%d", (unsigned int)mv_data[4] ) ;
					root["set_grade"] = s_set_grade;
					
					char s_set_state[10] = { 0x00 } ;
					sprintf( s_set_state, "%d", (unsigned int)mv_data[5] ) ;
					root["set_state"] = s_set_state;
					
					char s_set_count_down[10] = { 0x00 } ;
					char c_set_count_down = mv_data[6] & 0xf0;
					c_set_count_down >>= 4 ;
					sprintf( s_set_count_down, "%d", (unsigned int)c_set_count_down) ;
					root["set_count_down"] = s_set_count_down;
					
					char s_set_run_status[10] = { 0x00 } ;
					sprintf( s_set_run_status, "%d", (unsigned int)mv_data[6] & 0x0f ) ;
					root["set_run_status"] = s_set_run_status;
					break ;
				}
				case 0x86:		//5.2.5 5.2.6 5.2.7
				{
					//return ;//rui---------------
					switch( (int)mv_data.size() )
					{
						case 18:
						{
							char s_speed[10] = { 0x00 } ;
							sprintf( s_speed, "%.2f", (float)(((float)mv_data[3])/10) ) ;
							root["speed"] = s_speed;
							
							char s_grade[10] = { 0x00 } ;
							sprintf( s_grade, "%.1f", (float)(((float)mv_data[4])/10) ) ;
							root["grade"] = s_grade;

							unsigned short sh_time_elapse = mv_data[5] ;
							sh_time_elapse <<=8 ;
							sh_time_elapse|=mv_data[6] ;
							root["time_elapse"] =lexical_cast<string>( sh_time_elapse ) ;					

							unsigned short sh_distance = mv_data[7] ;
							sh_distance <<= 8 ;
							sh_distance |= mv_data[8] ;
							root["distance"] = lexical_cast<string>( sh_distance*10 ) ;
					
							unsigned short sh_kcal = mv_data[9] ;
							sh_kcal <<= 8 ;
							sh_kcal |= mv_data[10];
							root["kcal"] =  lexical_cast<string>( sh_kcal ) ;									

							unsigned short sh_step = mv_data[11];
							sh_step <<= 8 ;
							sh_step |= mv_data[12] ;
							root["step"] = lexical_cast<string>( sh_step ) ;		
					
							unsigned short sh_axunge_elapse = mv_data[13] ;
							sh_axunge_elapse <<= 8 ;
							sh_axunge_elapse |= mv_data[14] ;
							root["axunge_elapse"] = lexical_cast<string>( sh_axunge_elapse ) ;
					
							unsigned short sh_suger_elapse = mv_data[15];
							sh_suger_elapse <<= 8 ; 
							sh_suger_elapse |= mv_data[16];
							root["suger_elapse"] = lexical_cast<string>( sh_suger_elapse ) ;

							char s_heartbeat[10] = { 0x00 } ;
							sprintf( s_heartbeat, "%d", (unsigned int)mv_data[17] ) ;
							root["heartbeat"] = s_heartbeat;
							break;
						}
						case 7:
						{
							unsigned short sh_error_info = 0 ;
							memcpy(&mv_data[3], (char*)&sh_error_info, 2 ) ;				
							root["error_info"] = lexical_cast<string>( sh_error_info ) ;							
							
							unsigned short sh_status_value = 0 ;
							memcpy(&mv_data[5], (char*)&sh_status_value, 2 ) ;				
							root["status_value"] = lexical_cast<string>( sh_status_value ) ;
							break;
						}
						case 13:
						{
							unsigned short sh_current1 = 0 ;
							memcpy(&mv_data[3], (char*)&sh_current1, 2 ) ;				
							root["current1"] = lexical_cast<string>( sh_current1 ) ;
							
							unsigned short sh_current2 = 0 ;
							memcpy(&mv_data[5], (char*)&sh_current2, 2 ) ;				
							root["current2"] = lexical_cast<string>( sh_current2 ) ;
							
							unsigned short sh_current3 = 0 ;
							memcpy(&mv_data[7], (char*)&sh_current3, 2 ) ;				
							root["current3"] = lexical_cast<string>( sh_current3 ) ;
							
							unsigned short sh_current4 = 0 ;
							memcpy(&mv_data[9], (char*)&sh_current4, 2 ) ;				
							root["current4"] = lexical_cast<string>( sh_current4 ) ;
							
							unsigned short sh_current5 = 0 ;
							memcpy(&mv_data[11], (char*)&sh_current5, 2 ) ;				
							root["current5"] = lexical_cast<string>( sh_current5 ) ;
							
							break;
						}
						default:
						{
							b_flag = false ;
							rlog << "com protocol error : data error" << endl ;
							break;
						}
					}						

					break ;
				}
				default:
				{
					b_flag = false ;
					rlog << "com protocol error : command type = " ;
					printf( "0x%02x\n", mv_data[2]) ;				
					break ;
				}
			}			
		
			break ;
		}
/*		case 0x02 :
		{
			break ;
		}
*/		
		default:
		{
			b_flag = false ;
			rlog << "com protocol error : device type = " ;
			printf( "0x%02x0x%02x\n", mv_data[0], mv_data[1]) ;
			break ;
		}
		
	}
	
	if( !b_flag )
		root["error_reason"] = "uart protocol error" ;

	app_service::_instance().write(root.toStyledString());
}


